import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/result.dart';
import 'package:huxiaodui/api/model/alarm_message_model.dart';
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/config.dart';
import 'package:huxiaodui/components/gallery.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'alarm_message_push.dart';

class AlarmMessageInfos extends StatefulWidget {
  const AlarmMessageInfos({super.key, required this.alarm});
  final AlarmModel alarm;
  @override
  State<AlarmMessageInfos> createState() => _AlarmMessageInfosState();
}

class _AlarmMessageInfosState extends State<AlarmMessageInfos> {
  List<AlarmMessageModel> alarmMessages = [];

  @override
  void initState() {
    super.initState();

    Api.request(
      isToast: true,
      data: {"id": widget.alarm.id},
      url: AlarmUrl.allMessage,
      success: (Result res) {
        setState(
          () {
            alarmMessages = AlarmMessageModel.toList(res.data);
          },
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.alarm.unit.name),
      ),
      body: ListView.builder(
        itemCount: alarmMessages.length,
        itemBuilder: (BuildContext context, int index) {
          var controller = TextEditingController();
          controller.text = alarmMessages[index].content;

          List<GalleryItem> galleryItems = [];
          if (alarmMessages[index].paths.isNotEmpty) {
            for (var element in alarmMessages[index].paths) {
              galleryItems.add(
                GalleryItem(
                  id: element,
                  resource: Image.network("${ApiConfig.httpHost}/image/$element",
                      width: 200, height: 200, fit: BoxFit.cover),
                ),
              );
            }
          }

          return Container(
            padding:
                const EdgeInsets.only(top: 8, bottom: 5, left: 8, right: 8),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: <Widget>[
                        const SizedBox(
                          width: 32,
                          height: 32,
                          child: CircleAvatar(
                            radius: 50,
                            backgroundImage:
                                AssetImage('assets/images/black_car.jpg'),
                          ),
                        ),
                        const SizedBox(width: 5),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              alarmMessages[index].mobileApp.name,
                              style: const TextStyle(fontSize: 16),
                            ),
                            Text(
                                alarmMessages[index]
                                    .createdAt
                                    .toIso8601String()
                                    .split('.')[0]
                                    .replaceAll('T', ' '),
                                style: const TextStyle(
                                    fontSize: 12, color: Colors.grey)),
                          ],
                        ),
                      ],
                    ),
                    Row(
                      children: [

                      ],
                    )
                  ],
                ),
                Container(
                  padding: EdgeInsets.all(8),
                  child: Text(
                    alarmMessages[index].content,
                    textAlign: TextAlign.left,
                    maxLines: null,
                  ),
                ),
                Visibility(
                  visible: galleryItems.length > 0,
                  child: SizedBox(
                    child: GalleryComponents(
                      galleryItems: galleryItems,
                    ),
                  ),
                ),
                const Divider(
                  color: Colors.grey,
                  thickness: 1.0,
                ),
              ],
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          var r = await Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => AlarmMessagePush(alarm: widget.alarm)),
          );
          if (r == true) {
            Api.request(
              url: AlarmUrl.allMessage,
              data: {"id": widget.alarm.id},
              success: (Result res) {
                setState(
                  () {
                    alarmMessages = AlarmMessageModel.toList(res.data);
                  },
                );
              },
            );
          }
        },
        child: const Text("上传"),
      ),
    );
  }
}
